<?php
//if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Thumbs{
    var $width=NULL;
    var $height=NULL;
    var $type=NULL;
    var $width_new=NULL;
    var $height_new=NULL;
    var $imagen=NULL;
    var $imagen_new=NULL;
    var $source=NULL;

    function  __construct() {
        switch(func_num_args()){
            case 3:
                $this->setSource(func_get_arg(0));
                $this->setWidthNew(func_get_arg(1));
                $this->setHeightNew(func_get_arg(2));
                if($this->vThumb()){
                    $this->setInfo();
                    $this->printImg();
                }
                break;
            case 4:
                $aa= func_get_arg(1);
                $bb= func_get_arg(2);
                $cc= func_get_arg(3);
                $this->setSource(func_get_arg(0));
                $this->setWidthNew(func_get_arg(1));
                $this->setHeightNew(func_get_arg(2));
                if($this->vThumb()){
                    $this->setInfo();
                    $this->setDim($aa,$bb, $cc);
                    $this->printImg();
                }
                break;
        }
    }

    function setInfo(){
        $_x=getimagesize($this->getSource());
        $this->setWidth($_x[0]);
        $this->setHeight($_x[1]);
        $this->setType($_x[2]);
    }

    function setDim($_w,$_h,$_d){
        switch($_d){
            case 1:
                if($this->getWidth()<$this->getHeight()){
                    $this->setWidthNew($_h);
                    $this->setHeightNew($_w);
                }
            break;
            case 2:
                if($this->getWidth()<=$_w && $this->getHeight()<=$_h){
                    $this->setWidthNew($this->getWidth());
                    $this->setHeightNew($this->getHeight());
                }
                else{
                    if($this->getWidth()<$this->getHeight()){
                        $this->setWidthNew(round(($this->getWidth() / $this->getHeight()) * $_h));
                        //$this->setHeightNew($_w);
                    }
                }
            break;
            case 3:
                if($this->getWidth()<=$_w && $this->getHeight()<=$_h){
                    $this->setWidthNew($this->getWidth());
                    $this->setHeightNew($this->getHeight());
                }
                else{
                    //if($this->getWidth()<=$this->getHeight()){
                        $this->setHeightNew($_h);
                        $this->setWidthNew(round(($this->getWidth() / $this->getHeight()) * $_h));
                    //}
                    //else{
                    //    $this->setWidthNew($_w);
                    //    $this->setHeightNew(round(($this->getHeight() / $this->getWidth()) * $_w));
                    //}
                }
            break;
        }
        
    }

    function printImg(){
        $this->setImagenNew(imagecreatetruecolor($this->getWidthNew(),$this->getHeightNew()));
        
        switch($this->getType()){
            case 1:
                $this->setImagen(imagecreatefromgif($this->getSource()));
                 imagealphablending($this->getImagenNew(), false);
                imagesavealpha($this->getImagenNew(),true);
                $transparent = imagecolorallocatealpha($this->getImagenNew(), 255, 255, 255, 127);
                imagefilledrectangle($this->getImagenNew(), 0, 0, $this->getWidthNew(), $this->getHeightNew(), $transparent);
                imagecopyresampled($this->getImagenNew(), $this->getImagen(), 0, 0, 0, 0, $this->getWidthNew(), $this->getHeightNew(), $this->getWidth(), $this->getHeight());
                header("Content-type: image/gif");
                imagegif($this->getImagenNew());
                break;
            case 2:
                $this->setImagen(imagecreatefromjpeg($this->getSource()));
                imagecopyresampled($this->getImagenNew(), $this->getImagen(), 0, 0, 0, 0, $this->getWidthNew(), $this->getHeightNew(), $this->getWidth(), $this->getHeight());
                header("Content-type: image/jpeg");
                imagejpeg($this->getImagenNew(),NULL,100);
                break;
            case 3:
                $this->setImagen(imagecreatefrompng($this->getSource()));
                imagealphablending($this->getImagenNew(), false);
                imagesavealpha($this->getImagenNew(),true);
                $transparent = imagecolorallocatealpha($this->getImagenNew(), 255, 255, 255, 127);
                imagefilledrectangle($this->getImagenNew(), 0, 0, $this->getWidthNew(), $this->getHeightNew(), $transparent);
                imagecopyresampled($this->getImagenNew(), $this->getImagen(), 0, 0, 0, 0, $this->getWidthNew(), $this->getHeightNew(), $this->getWidth(), $this->getHeight());
                header("Content-type: image/x-png");
                imagepng($this->getImagenNew(),NULL,9);
                break;
        }        
    }

    function vThumb(){
        //$val=New Validaciones();
        if(file_exists($this->getSource()))
        //if($val->extFile($this->getSource(), 'jpg'))
            return true;
        //if($val->numeros($this->getWidthNew())&&$this->getWidthNew()>50&&$this->getWidthNew()<1000)
        //if($val->numeros($this->getHeightNew())&&$this->getHeightNew()>50&&$this->getHeightNew()<1000)
        return false;
    }

    /*  SETS  */
    function setWidth($_x)      { $this->width=$_x; }
    function setHeight($_x)     { $this->height=$_x; }
    function setType($_x)       { $this->type=$_x; }
    function setWidthNew($_x)   { $this->width_new=$_x; }
    function setHeightNew($_x)  { $this->height_new=$_x; }
    function setImagen($_x)     { $this->imagen=$_x; }
    function setImagenNew($_x)  { $this->imagen_new=$_x; }
    function setSource($_x)     { $this->source=$_x; }

    /*  GETS  */
    function getWidth()         { return $this->width; }
    function getHeight()        { return $this->height; }
    function getType()          { return $this->type; }
    function getWidthNew()      { return $this->width_new; }
    function getHeightNew()     { return $this->height_new; }
    function getImagen()        { return $this->imagen; }
    function getImagenNew()     { return $this->imagen_new; }
    function getSource()        { return $this->source; }
}
//if(isset($_GET['s'])&&isset($_GET['w'])&&isset($_GET['h']))
    new Thumbs($_GET['s'],$_GET['w'],$_GET['h'],$_GET['d']);

?>
